<template>
  <el-row class="tac">
    <el-col :span="6">
      <el-menu
          active-text-color="#ffd04b"
          background-color="#545c64"
          class="el-menu-vertical-demo"
          default-active="2"
          text-color="#fff"
          @open="handleOpen"
          @close="handleClose"
      >
        <el-sub-menu index="1">
          <template #title>
            <el-icon>
              <location/>
            </el-icon>
            <span>权限管理</span>
          </template>
          <el-menu-item-group v-for="(item,key) in menuList" :key="key" :title="item.resource_name">
            <el-menu-item :index="key" @click="handleMenuItemClick(key, item)">{{ item.resource_name }}</el-menu-item>
          </el-menu-item-group>
        </el-sub-menu>
      </el-menu>
    </el-col>
    <el-col span="18">
      <div v-if="name == '资源管理'">
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <el-button @click="toadd" type="primary">添加</el-button>
        <el-table :data="menuList">
          <el-table-column prop="resource_name" label="管理名称"/>
          <el-table-column label="操作">
            <template #default>
              <el-button link type="primary" size="small">
                修改
              </el-button>
              <el-button link type="primary" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-dialog
            v-model="dialogVisible"
            title="添加管理"
            width="500"
        >
          <span>
            <el-input v-model="resources.resource_name" style="width: 240px"  placeholder="管理名称" />
          </span>
          <template #footer>
            <div class="dialog-footer">
              <el-button @click="dialogVisible = false;resources= {'url':0,
                                                                         'parent':1}">取消</el-button>
              <el-button type="primary" @click="add">
                添加
              </el-button>
            </div>
          </template>
        </el-dialog>
      </div>
    </el-col>
  </el-row>
</template>
<script lang="ts" setup>
import {onMounted, ref} from "vue";

let menuList = ref([])
let name = ref("")
let dialogVisible = ref(false)
let resources = ref({
  "url":0,
  "parent":1
})
import {
  Document,
  Menu as IconMenu,
  Location,
  Setting,
} from '@element-plus/icons-vue'
import axios from "axios";
import {k} from "vite/dist/node/types.d-aGj9QkWt";
import {ElMessage} from "element-plus";

const handleOpen = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}
const handleClose = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}
const findall = () => {
  axios.get(`http://localhost:8000/tadmin/resourcesall/`).then(res => {
    menuList.value = res.data.data
  })
}
const handleMenuItemClick = (key: string, item: any) => {
  name.value = item.resource_name
};
const toadd =  () =>{
  dialogVisible.value=true
}
const add = () => {
  axios.post(`http://localhost:8000/tadmin/resourcesadd/`,resources.value).then(res=>{
    console.log(resources.value)
    if(res.data.code==200){
      ElMessage.success(res.data.msg)
    }else {
      ElMessage.error(res.data.msg)
    }
    resources.value=resources.value= {'url':0,
      'parent':1}
    dialogVisible.value=false
    findall()
  })
}
onMounted(() => {
  findall()
})
</script>
